package com.woniu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.entity.ProApprove;
import com.woniu.entity.Project;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


public interface ProApproveMapper extends BaseMapper<ProApprove> {
    @Insert("insert into pro_approve(pro_id,dept_id,app_pro_name,app_pro_contract,app_pro_end_time,app_pro_pre_price,app_pro_status,app_feedback,app_apply_time,app_approve_time) values(#{proId},#{deptId},#{appProName},#{appProContract},#{appProEndTime},#{appPrePrice},#{appProStatus},#{appFeedback},#{appApplyTime},#{appApproveTime})")
    @Options(useGeneratedKeys = true,keyColumn ="approve_id",keyProperty ="approveId")
    void add(ProApprove proApprove);


    @Select("select * from pro_approve")
    List<ProApprove> getAll();



    @Select("select * from pro_approve  where  approve_id=#{approveId}")//project p,,dept d  p.pro_id=pa.pro_id and pa.dept_id=d.dept_id and
    @Results(id="proApproveMap",value={
            @Result(column = "approve_id",property = "approveId",id = true),
            @Result(column = "pro_id",property = "project.proId"),
            @Result(column = "dept_id",property = "dept.deptId"),
            @Result(column = "app_pro_name",property = "appProName"),
            @Result(column = "app_pro_contract",property = "appProContract"),
            @Result(column = "app_pro_end_time",property = "appProEndTime"),
            @Result(column = "app_pre_price",property = "appPrePrice"),
            //@Result(column = "app_pro_left_price",property = "appProLeftPrice"),
            @Result(column = "app_pro_status",property = "appProStatus"),
            @Result(column = "app_feedback",property = "appFeedback"),
            @Result(column = "app_apply_time",property = "appApplyTime"),
            @Result(column = "app_approve_time",property = "appApproveTime"),
    })
    ProApprove getByProApproveId(int approveId);

    @Select("select * from project p,pro_approve pa where p.pro_id =pa.pro_id  and pa.pro_id=#{project.proId}")//,pro_mid_pro_approve pmpa  and pa.approve_id=pmpa.approve_id
    @ResultMap("proApproveMap")
    List<ProApprove> getProApproveByProId(int proId);

    @SelectProvider(type = ProApproveMapper.ProApproveSqlProvider.class, method = "select")
    List<ProApprove> getByCondition(ProApprove proApprove);


    @Select("select count(*) from pro_approve where app_pro_status=#{appProStatus} and pro_id in(select pro_id from project where pro_id=#{proId})")
    @ResultMap("proApproveMap")
    Integer getNumsByProStatusAndProId(Project project,String appProStatus);


    @Select("select * from pro_approve where app_pro_status=#{appProStatus} and pro_id in(select pro_id from project where pro_id=#{proId})")
    @ResultMap("proApproveMap")
    List<ProApprove> getProApproveByProStatusAndProId(Project project,String appProStatus);

    @UpdateProvider(type = ProApproveMapper.ProApproveSqlProvider.class, method = "update")
    void update(ProApprove proApprove);


    //根据项目id查审批表状不通过
    @Select("select * from pro_approve pa,project p WHERE  app_pro_status = 'n' and p.pro_id =pa.pro_id and pro_id=#{proId})")
    ProApprove getProAppNotPassByProId(int proId);

    //根据项目id查审批表状态审批中
    @Select("select * from pro_approve pa,project p WHERE  app_pro_status = 'r' and p.pro_id =pa.pro_id and pro_id=#{proId})")
    ProApprove getProAppProcessingByProId(int proId);

    //根据用户ID,以及审批表状态来查项目
    @Select("select * from pro_approve WHERE  app_pro_status = 'n' and pro_id in (select pro_id from project where user_id = #{uid})")
    List<ProApprove> getByUid(int uid);

    //创建审核表（提交项目时）
    @Insert("insert into pro_approve values(#{approveId},#{project.proId},#{dept.deptId},#{appProName},#{appProContract},#{appProEndTime},#{appPrePrice},'r',#{appFeedback},#{appApplyTime},#{appApproveTime})")
    void insertProApprove(ProApprove proApprove);

    //重新提交审核表(审批未通过)
    @Update("update pro_approve set pro_id = #{project.proId}, dept_id = #{dept.deptId}, app_pro_name = #{appProName}, app_pro_contract = #{appProContract}, app_pro_end_time = #{appProEndTime}, app_pre_price = #{appPrePrice}, app_pro_status = 'r' where approve_id = #{approveId}")
    void reSubmitProApprve(ProApprove proApprove);

    //审核通过
    @Update("update pro_approve set app_feedback = #{afeedback},app_approve_time = #{aatime},app_pro_status = 'y' where pro_id = #{proid}")
    void Apass(@Param("proid") int proid, @Param("aatime") String aatime,@Param("afeedback") String afeedback);

    //审核未通过
    @Update("update pro_approve set app_feedback = #{afeedback},app_approve_time = #{aatime},app_pro_status = 'n' where pro_id = #{proid}")
    void Aunpass(@Param("proid") int proid, @Param("aatime") String aatime,@Param("afeedback") String afeedback);

//    Integer getNumsByProApproveId(int approveid);

    @Slf4j
    public  class ProApproveSqlProvider {
        public static String update(ProApprove proApprove) {

            return new SQL() {
                {
                    UPDATE("pro_approve");
                    if(proApprove.getProject()!=null&&!"".equals(proApprove.getProject().toString().trim())){
                        SET("pro_id=#{proId}");
                    }
                    if(proApprove.getDept()!=null&&!"".equals(proApprove.getDept().toString().trim())){
                        SET("dept_id=#{deptId}");
                    }
                    if(proApprove.getAppProName()!=null&&!"".equals(proApprove.getAppProName().toString().trim())){
                        SET("app_pro_name=#{appProName}");
                    }
                    if(proApprove.getAppProContract()!=null&&!"".equals(proApprove.getAppProContract().toString().trim())){
                        SET("app_pro_contract=#{appProContract}");
                    }
                    if(proApprove.getAppProEndTime()!=null&&!"".equals(proApprove.getAppProEndTime().toString().trim())){
                        SET("app_pro_end_time=#{appProEndTime}");
                    }
                    if(proApprove.getAppPrePrice()!=null&&!"".equals(proApprove.getAppPrePrice().toString().trim())){
                        SET("app_pre_price=#{appPrePrice}");
                    }
//                    if(proApprove.getAppProLeftPrice()!=null&&!"".equals(proApprove.getAppProLeftPrice().toString().trim())){
//                        SET("app_pro_left_price=#{appProLeftPrice}");
//                    }
                    if(proApprove.getAppProStatus()!=null&&!"".equals(proApprove.getAppProStatus().toString().trim())){
                        SET("app_pro_status=#{appProStatus}");
                    }
                    if(proApprove.getAppFeedback()!=null&&!"".equals(proApprove.getAppFeedback().toString().trim())){
                        SET("app_feedback=#{appFeedback}");
                    }
                    if(proApprove.getAppApplyTime()!=null&&!"".equals(proApprove.getAppApplyTime().toString().trim())){
                        SET("app_apply_time=#{appApplyTime}");
                    }
                    if(proApprove.getAppApproveTime()!=null&&!"".equals(proApprove.getAppApproveTime().toString().trim())){
                        SET("app_approve_time=#{appApproveTime}");
                    }
                    WHERE("approve_id=#{approveId}");
                }
            }.toString();
            //log.debug("SQL:{}",sql);
        }

        public static String select(ProApprove proApprove) {
            return new SQL() {{
                SELECT("*");
                FROM("pro_approve");
                if (proApprove.getApproveId() != 0) {
                    WHERE("approve_id=#{approveId}");
                }
                if (proApprove.getProject() != null&&!"".equals(proApprove.getProject().toString().trim())) {
                    WHERE("pro_id=#{proId}");
                }
                if (proApprove.getDept() != null&&!"".equals(proApprove.getDept().toString().trim())) {
                    WHERE("dept_id=#{deptId}");
                }
                if (proApprove.getAppProName() != null&&!"".equals(proApprove.getAppProName().toString().trim())) {
                    WHERE("app_pro_name=#{appProName}");
                }
                if (proApprove.getAppProContract() != null&&!"".equals(proApprove.getAppProContract().toString().trim())) {
                    WHERE("app_pro_contract=#{appProContract}");
                }
                if (proApprove.getAppProEndTime() != null&&!"".equals(proApprove.getAppProEndTime().toString().trim())) {
                    WHERE("app_pro_end_time=#{appProEndTime}");
                }
                if (proApprove.getAppPrePrice() != null&&!"".equals(proApprove.getAppPrePrice().toString().trim())) {
                    WHERE("pro_pre_price=#{proEndTime}");
                }
//                if (proApprove.getAppProLeftPrice() != null&&!"".equals(proApprove.getAppProLeftPrice().toString().trim())) {
//                    WHERE("app_pro_left_price=#{appProLeftPrice}");
//                }
                if (proApprove.getAppProStatus() != null&&!"".equals(proApprove.getAppProStatus().toString().trim())) {
                    WHERE("app_pro_status=#{appProStatus}");
                }
                if (proApprove.getAppFeedback() != null&&!"".equals(proApprove.getAppFeedback().toString().trim())) {
                    WHERE("app_feedback=#{appFeedback}");
                }
                if (proApprove.getAppApplyTime() != null&&!"".equals(proApprove.getAppApplyTime().toString().trim())) {
                    WHERE("app_apply_time=#{appApplyTime}");
                }
                if (proApprove.getAppApproveTime() != null&&!"".equals(proApprove.getAppApproveTime().toString().trim())) {
                    WHERE("app_approve_time=#{appApproveTime}");
                }
                if (proApprove.getStartTime() != null&&!"".equals(proApprove.getStartTime().toString().trim())) {
                    WHERE("app_apply_time &gt;=#{startTime}");
                }
                if (proApprove.getEndTime() != null&&!"".equals(proApprove.getEndTime().toString().trim())) {
                    WHERE("app_apply_time &lt;=#{endTime}");
                }
                ORDER_BY("approve_id");
            }}.toString();
        }
    }
}
